
package org.jackysoft.struts2.admin;

import java.util.Collection;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Namespace;
import org.jackysoft.entity.OfficeDocument;
import org.jackysoft.entity.OfficeTemplate;
import org.jackysoft.service.OfficeTemplateService;
import org.jackysoft.struts2.workflow.AbstractWorkflowAction;
import org.jackysoft.util.HttpUtil;
import org.jackysoft.util.IOUtils;
import org.jackysoft.util.StringUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zhuozhengsoft.pageoffice.FileSaver;
/**
 * 该类用来协助保存处理在线office文件的 操作
 * 其操作形式主要依赖于所用的在线office 控件
 * 现在我们依赖的是科翰公司的SOAOffice,所以
 * 使用了SOAOffice的API
 * */
@Controller
@Namespace("/admin/office")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class OfficeManagerAction
extends AbstractWorkflowAction<OfficeTemplate>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 9027507964654421758L;

	private Collection<OfficeTemplate> beans;
	private OfficeTemplate bean=null;
	private OfficeDocument office = null;
	private OfficeTemplateService service;	
	private String saveUrl;
	private String openUrl;
	
	@Override
	public String add() throws Exception {
		service.create(upLoad, upLoadFileName);		
		return SUCCESS;
	}
	

	@Override	
	public String update() throws Exception {
		service.update(bean, upLoad, upLoadFileName);
		return SUCCESS;
	}

	public String viewdoc() throws Exception {	
		OfficeDocument odt =  workflowService.getOffice(workflowId);
		this.setContentType(HttpUtil.extToMimeType(odt.getExt()));
		this.setContentLength(odt.getData().length);
		this.setUpLoadFileName("filename.doc");
		inputStream = IOUtils.toInputStream(odt.getData());
		return STREAM;
	}
	/**
	 * 显示模板内容并 生成控件的saveurl;
	 */	
	public String viewtpl() throws Exception {
		OfficeTemplate ot = service.get(bean.getId());
		this.setContentType(HttpUtil.extToMimeType(ot.getExt()));
		this.setContentLength(ot.getData().length);
		this.setUpLoadFileName(StringUtils.toDownloadFileName(ot.getName()));
		inputStream = IOUtils.toInputStream(ot.getData());
		return STREAM;
	}
	/**
	 * 保存office的内容
	 */	
	public String savedoc() throws Exception {
		FileSaver fs = new FileSaver(httpRequest,httpResponse);
		byte[] data  = fs.getFileBytes();
		workflowService.updateDocument(data, workflowId);
		httpRequest.setAttribute("fs", fs);
		return "savedoc";
	}
	
	/**
	 * 保存office的内容
	 */	
	public String collatedoc() throws Exception {
		FileSaver fs = new FileSaver(httpRequest,httpResponse);
		byte[] data  = fs.getFileBytes();
		workflowService.collate(data,office.getExt(),workflowId);
		return "saveok";
	}

	/**
	 * <dd>列出模板选择如果该工作流</dd>
	 * <dd>已经有了OFFICE模板实例,那么就</dd>
	 * <dd>直接打开,否则,列出模板让大家选</dd>
	 * <dd>择
	 */	
	public String preselect() throws Exception {
		
		setSaveUrl("admin/office/office-manager!savedoc");
		setOpenUrl("admin/office/office-manager!viewdoc");		
		office = workflowService.getOffice(workflowId);		
		if(office==null){
			beans = service.gets();
			return "prepare";
		}
		return "viewdoc";
	}
	
	public String viewcollate() throws Exception{
		setSaveUrl("admin/office/office-manager!collatedoc");
		setOpenUrl("admin/office/office-manager!viewdoc");
		office = workflowService.getOffice(workflowId);		
		setting = entitySettingService.get(workflowId, actionId, actionItem.getStepId());
		return "viewcollate";
	}

 	
	public OfficeDocument getOffice() {
		return office;
	}

	public void setOffice(OfficeDocument office) {
		this.office = office;
	}


	public Collection<OfficeTemplate> getBeans() {
		return beans;
	}


	public void setBeans(Collection<OfficeTemplate> beans) {
		this.beans = beans;
	}


	public OfficeTemplate getBean() {
		return bean;
	}


	public void setBean(OfficeTemplate bean) {
		this.bean = bean;
	}


	public OfficeTemplateService getService() {
		return service;
	}


	@Resource
	public void setService(OfficeTemplateService service) {
		this.service = service;
		this.setDataProvider(service);
	}

	public String getSaveUrl() {
		return saveUrl;
	}


	public void setSaveUrl(String saveUrl) {
		this.saveUrl = saveUrl;
	}


	public String getOpenUrl() {
		return openUrl;
	}


	public void setOpenUrl(String openUrl) {
		this.openUrl = openUrl;
	}   
}